//
//  CoinRecordViewController.m
//  AppleMarket
//
//  Created by yangy on 02/11/2018.
//  Copyright © 2018 szgsy. All rights reserved.
//

#import "CoinRecordViewController.h"

// view
#import "CoinRecordFilterView.h"
// model
#import "CoinAssetModel.h"
#import "AssetRecordModel.h"
// controller
#import "CoinRecordDetailViewController.h"

@interface CoinRecordViewController ()<UITableViewDelegate,UITableViewDataSource,CoinRecordFilterViewDelegate>
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataList;
@property (nonatomic, strong) CoinRecordFilterView *filterView;
@property (nonatomic, assign) NSInteger curPage;
@property (nonatomic, assign) NSInteger pageCount;
@property (nonatomic, assign) NSInteger coinIndex;
@property (nonatomic, assign) NSInteger typeIndex;
@end

@implementation CoinRecordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"交易记录";
    [self initViews];
    [self setNagivationBar];
}

- (void) initViews
{
    [self.view addSubview:self.tableView];
    
    [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
}

- (void) setNagivationBar
{
    UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightButton setImage:[UIImage imageNamed:@"icon_filter"] forState:UIControlStateNormal];
    [[rightButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [self.filterView showInView:self.view];
    }];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
}

- (void) refersh
{
    [NetRequest request_GetUserAssetRecordListWithId:self.coinIndex == 0? 0 : self.coinModels[_coinIndex].CurrencyID typeId:self.typeIndex == 5? 6 : self.typeIndex  pageIndex:self.curPage pageSize:20 completeHandler:^(id  _Nonnull responseObj) {
        [self.tableView.mj_header endRefreshing];
        if([[responseObj objectForKey:@"Status"] boolValue]){
            NSDictionary *data = [responseObj objectForKey:@"Data"];
            NSArray *dataList = [data objectForKey:@"DataList"];
            self.dataList = [[[dataList.rac_sequence map:^id _Nullable(id  _Nullable value) {
                AssetRecordModel *model = [AssetRecordModel yy_modelWithDictionary:value];
                return model;
            }] array] mutableCopy];
            self.curPage ++;
            self.pageCount = [[data objectForKey:@"TotalCount"] integerValue];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
    }];
}

- (void) loadMore
{
    [NetRequest request_GetUserAssetRecordListWithId:self.coinIndex == 0? 0 : self.coinModels[_coinIndex].CurrencyID typeId:self.typeIndex  pageIndex:self.curPage pageSize:20 completeHandler:^(id  _Nonnull responseObj) {
        [self.tableView.mj_footer endRefreshing];
        if([[responseObj objectForKey:@"Status"] boolValue]){
            NSDictionary *data = [responseObj objectForKey:@"Data"];
            NSArray *dataList = [data objectForKey:@"DataList"];
            NSArray *modes = [[dataList.rac_sequence map:^id _Nullable(id  _Nullable value) {
                AssetRecordModel *model = [AssetRecordModel yy_modelWithDictionary:value];
                return model;
            }] array];
            [self.dataList addObjectsFromArray:modes];
            self.curPage ++;
            self.pageCount = [[data objectForKey:@"TotalCount"] integerValue];
            [self.tableView reloadData];
        }
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_footer endRefreshing];
    }];
}

#pragma mark - UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([UITableViewCell class])];
    if(cell == nil){
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:NSStringFromClass([UITableViewCell class])];
        cell.textLabel.font = kFontBold(17);
        cell.textLabel.textColor = kColorWithHex(0x0E304C);
        cell.detailTextLabel.textColor = kColorWithHex(0x808C99);
        cell.detailTextLabel.font = kFont(12);
        if(![cell.contentView viewWithTag:100]){
            UILabel *label = [UILabel cm_labelWithText:@"" font:kFont(15) textColor:kColorWithHex(0x4A647A) textAlign:NSTextAlignmentRight];
            label.tag = 100;
            label.frame = CGRectMake(self.view.bounds.size.width - 215, 32.5, 200, 15);
            [cell.contentView addSubview:label];
        }
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    
    AssetRecordModel *u = self.dataList[indexPath.row];

    cell.textLabel.text = [NSString stringWithFormat:@"%@%@",u.RecordTypeName,u.CurrencyUnit];
    cell.detailTextLabel.text = u.CreateTime;

    UILabel *statusLabel = [cell.contentView viewWithTag:100];
    if(statusLabel){
        statusLabel.text = [u.ChangeValue floatValue] >= 0? [NSString stringWithFormat:@"+%@",u.ChangeValue] : u.ChangeValue;
        statusLabel.textColor = [u.ChangeValue floatValue] >=0 ? kColorWithHex(0x3291EB) : kColorWithHex(0x0E304C);
    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AssetRecordModel *u = self.dataList[indexPath.row];
    CoinRecordDetailViewController *detailVC = [[CoinRecordDetailViewController alloc] init];
    detailVC.assetModel = u;
    [self bh_PushViewController:detailVC hiddenTabbar:YES];
}

#pragma mark - CoinRecordFilterViewDelegate
- (void) filterView:(id) filterView didSearchCoinIndex:(NSInteger) index typeIndex:(NSInteger) typeIndex
{
    self.coinIndex = index;
    self.typeIndex = typeIndex;
    self.curPage = 1;
    [self refersh];
}

#pragma mark - lazy load
- (UITableView *)tableView
{
    if(!_tableView){
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        [_tableView setDelegate:self];
        [_tableView setDataSource:self];
        [_tableView setSeparatorColor:kColorWithHex(0xEFEFEF)];
        [_tableView setSeparatorInset:UIEdgeInsetsMake(0, 15, 0, 15)];
        [_tableView setRowHeight:80];
        [_tableView setTableFooterView:[UIView new]];
        [_tableView setMj_header:[MJRefreshNormalHeader headerWithRefreshingBlock:^{
            self.curPage = 1;
            [self refersh];
        }]];
        [_tableView setMj_footer:[MJRefreshAutoFooter footerWithRefreshingBlock:^{
            if(self.curPage < self.pageCount){
                [self loadMore];
            }
        }]];
        [_tableView.mj_header beginRefreshing];
    }
    return _tableView;
}

- (CoinRecordFilterView *)filterView
{
    if(!_filterView){
        NSMutableArray *coinNameArr = [[[self.coinModels.rac_sequence map:^id _Nullable(CoinAssetModel * _Nullable value) {
            return value.Unit;
        }] array] mutableCopy] ;
        [coinNameArr insertObject:@"所有" atIndex:0];
        NSArray *typeArr = @[@"所有",@"充值",@"提现",@"买入",@"卖出",@"资金划转"];
        _filterView = [[CoinRecordFilterView alloc] initWithFrame:self.view.bounds coinArr:coinNameArr type:typeArr];
        _filterView.delegate = self;
    }
    return _filterView;
}

@end
